﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms.Design;
using System.Security.Permissions;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;

namespace ControlsLib
{
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    class DataFormControlDesigner3 : ParentControlDesigner
    {
        #region Storage

        private DataFormControl3 _MBP;
        private DesignerVerbCollection _Verbs;

        #endregion

        #region Overrides

        public override void Initialize(IComponent component)
        {
            _MBP = component as DataFormControl3;

            base.Initialize(component);

            AddElements();
        }

        #endregion

        #region " Private methods "

        private void AddElements()
        {
            IDesignerHost DH = (IDesignerHost)GetService(typeof(IDesignerHost));
            if (DH != null)
            {
                DesignerTransaction DT = null;
                
                try
                {
                    DT = DH.CreateTransaction("Choosing Type");

                    Control.ControlCollection OldControls = _MBP.Controls;
                    RaiseComponentChanging(TypeDescriptor.GetProperties(typeof(Control))["Controls"]);

                    BindingSource m_bs = ((AnagraficaManagerBase)DH.RootComponent).BindingSource;
                    Type m_selectedType = ((AnagraficaManagerBase)DH.RootComponent).SelectedType;
                    this._MBP.Container.Add(m_bs);

                    object OldDatasource = m_bs.DataSource;
                    RaiseComponentChanging(TypeDescriptor.GetProperties(typeof(BindingSource))["Datasource"]);

                    if (m_bs.DataSource != null)
                    {
                        int step = 5;
                        int leftOffset = 5;
                        DataFormControlHelper dfch = new DataFormControlHelper(DH, m_bs);
                        foreach (System.Reflection.PropertyInfo pi in m_selectedType.GetProperties()) // m_bs.DataSource.GetType().GetProperties())
                        {
                            //Label l = new Label();
                            Label l = DH.CreateComponent(typeof(Label)) as Label;
                            l.Location = new System.Drawing.Point(leftOffset, step);
                            l.Text = string.Format("{0}:", pi.Name);

                            //TextBox txt = new TextBox();
                            Control txt = dfch.GetSpecificControl(pi.PropertyType);
                            txt.Location = new System.Drawing.Point(leftOffset + l.Width + 5, step);

                            // DataBinding...
                            ControlBindingsCollection OldDataBindings = txt.DataBindings;
                            RaiseComponentChanging(TypeDescriptor.GetProperties(typeof(Binding))["DataBindings"]);
                            txt.DataBindings.Add(new Binding("Text", m_bs, pi.Name));
                            RaiseComponentChanged(TypeDescriptor.GetProperties(typeof(Binding))["DataBindings"], OldDataBindings, txt.DataBindings);

                            //
                            step += l.Size.Height;

                            //
                            _MBP.Controls.Add(l);
                            _MBP.Controls.Add(txt);

                        }
                    }

                    RaiseComponentChanged(TypeDescriptor.GetProperties(typeof(BindingSource))["Datasource"], OldDatasource, m_bs.DataSource);
                    RaiseComponentChanged(TypeDescriptor.GetProperties(typeof(Control))["Controls"], OldControls, _MBP.Controls);
                }
                catch (Exception ex)
                {
                    DisplayError(ex);
                    DT.Cancel();
                }
                finally
                {
                    DT.Commit();
                }
            }
        }

        #endregion
    }
}
